import React from "react";
import styles from "./index.module.less";
import SvgIcon from "@components/SvgIcon";
import { connect } from "react-redux";
import { addSearchValue } from "@redux/actions02";
import { profile } from "@depjs/storage";
import { request } from "@apis/request";

const AddUserSearchItem = (props) => {
    const { val, getSearchResult, showNoUser, showBounced } = props;

    const searchUser = async () => {
        if (val === profile.userName || parseInt(val) === profile.uid) {
            getSearchResult({
                isExist: true,
                isFriend: true,
                ...profile,
            });
            showNoUser(false);
            showBounced(true);
            return;
        }
        const res = await request("/chat/searchBeforeAdd", { val });
        if (res.code === "0000") {
            getSearchResult(res.data);
            if (res.data.isExist) {
                showNoUser(false);
                showBounced(true);
            } else {
                showNoUser(true);
                showBounced(false);
            }
        }
    };

    return (
        <div
            className={styles.add_user_search_item}
            onClick={(e) => {
                e.stopPropagation();
                searchUser();
            }}
        >
            <div className={styles.search_icon}>
                <SvgIcon font="icon-search-copy" />
            </div>
            <div className={styles.content}>
                搜索：
                <p className={styles.text}>{val}</p>
            </div>
            <div className={styles.arrow_icon}>
                <SvgIcon font="icon-xiangshang-copy" />
            </div>
        </div>
    );
};

export default connect((state) => ({ val: state.addSearchValReducer }), {
    addSearchValue,
})(AddUserSearchItem);
